<?php
/**
 * ThinkAPK基类
 * 
 * @copyright 	Copyright (c) 2011 thinkapk.com All rights reserved.
 * @package 	ThinkAPK
 * @link 		http://www.thinkapk.com/
 * @author 		richie <richbossman@163.com>
 * @version 	$Id: ThinkAPK.class.php 7610 2011-06-03 07:41:08Z richie $
 *
 */
class ThinkAPK extends Think {
	
	/**
	 * 格式化商品价格
	 *
	 * @param float $price
	 */
	public function formatPrice($price=0){
		$price = number_format ( $price, 2, '.', '' );
		return sprintf ( C('currency_format'), $price );
	}
	
	/**
	 * 图片的真实路径
	 *
	 * @param string $image
	 * @param string $dir
	 * @return string
	 */
	public function getImagePath($image='', $dir=''){
		if (empty ( $image )) {
			return '';
		}
		$pase = parse_url ( $image );
		if (! isset ( $pase ['scheme'] ) || empty ( $pase ['scheme'] )) {
			return '';
		}
		return $image;
	}
	
	//取得真实的ICON地址
	public function getIconPath($image=''){
		if(empty($image)){
			return STATIC_DOMAIN.'images/theme/noicon.png';
		}
		return $image;
	}
	
	//格式化时间
	public function formatTime($times=0, $showdate=false){
		if(!$times) return '';
		$time = time()-$times;
		if($time<0) return '';
		if($time<60){
			return $time.'秒前';
		}elseif($time < (60*60)){
			return ceil($time/(60)).'分钟前';
		}elseif($time < (60*60*24)){
			return ceil($time/(60*60)).'小时前';
		}else{
			return ($showdate==true)? date('Y-m-d', $times) : ((($time > (5*24*3600)) ? date('m-d', $times) : ceil($time/(60*60*24)).'天前'));
		}
	}
	
	/**
	 * 清除所有应用缓存（可指定清除的缓存名称）
	 *
	 * @param string $tag
	 * @return bool
	 */
	protected function clearCache($tag=''){
		$apps = require_once CONFIG_COMMON_PATH.'apps.php';
		$runtime_path = THINKAPK_PATH.RUNTIME_DIR.'/';
		//清除指定名称的缓存
		if($tag!=''){
			foreach ($apps as $app){
				$path = $runtime_path.$app.'/'.TEMP_DIR.'/';
				$name = '~@'.md5($tag).'.php';
				if(is_file($path.$name)) unlink($path.$name);
			}
		//清除所有缓存
		}else{
			foreach ($apps as $app){
				$path = $runtime_path.$app.'/'.TEMP_DIR.'/';
				if ( $dir = opendir( $path ) ){
		            while ( $file = readdir( $dir ) ){
		                $check = is_dir( $file );
		                if ( !$check )
		                    unlink( $path . $file );
		            }
		            closedir( $dir );
		        }
			}
		}
		unset($apps, $runtime_path, $tag);
		return true;
	}
}
?>